package com.nw.realm;



import com.nw.dao.UserDao;
import com.nw.entity.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class MyRealm extends AuthorizingRealm {
    @Resource
    UserDao userDao;
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //强转
        UsernamePasswordToken tokens = (UsernamePasswordToken)authenticationToken;
        //获取token中的身份信息
        String username = tokens.getUsername();
        User userFromDatabase = userDao.queryByUsername(username);
        System.out.println("数据库的用户信息为:"+userFromDatabase);

        SimpleAuthenticationInfo info = null;
        if(userFromDatabase != null){
            //创建info对象   数据库取出的信息  身份信息   凭证信息   随机盐  realm名
            info = new SimpleAuthenticationInfo(
                    userFromDatabase.getUsername(),userFromDatabase.getPassword(),this.getName()
            );
        }

        return info;
    }


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
